package com.spzx.product.controller;


import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/category")
@Tag(name = "商品分类")
public class CategoryController extends BaseController {

    @Resource
    CategoryService categoryService;

    //获取下拉列表
    @Operation(summary = "获取商品分类下拉列表")
    @GetMapping(value = "treeSelect/{id}")
    public AjaxResult treeSelect(@PathVariable("id") Long id){
        return success(categoryService.treeSelect(id));
    }


    //将数据导出的方法
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        categoryService.exportData(response);
    }

    //将数据导入的方法
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        try {
            categoryService.importData(file);
            return AjaxResult.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("导入失败");
    }

    @InnerAuth
    @Operation(summary = "内部调用接口：查询分类树")
    @GetMapping("listCatesTree")
    public R<List<Category>> listCatesTree(){
        return R.ok(categoryService.selectCatesTree());
    }
}
